package com.supinfo.suptracking.dao.jpa;

import com.supinfo.suptracking.dao.UserDao;
import com.supinfo.suptracking.entity.Car;
import com.supinfo.suptracking.entity.Invoice;
import com.supinfo.suptracking.entity.User;
import com.supinfo.suptracking.entity.User_;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author Fabien, Jérémy, Ludovic, Brice
 */

@Stateless
public class JpaUserDao implements UserDao{

    @PersistenceContext
    private EntityManager em;
    
    @Override
    public void addUser(User user) {
        em.persist(user);
    }

    @Override
    public void delUser(User user) {
        Long id = user.getId();
        em.remove(id);
    }

    @Override
    public void delUser(Long id) {
        em.remove(id);
    }

    @Override
    public void updateUser(User user) {
        em.merge(user);
    }

    @Override
    public boolean isAdminUser(User user) {
        return user.isUserAdmin();
    }

    @Override
    public List<User> getAllUser() {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<User> query = cb.createQuery(User.class);
        return em.createQuery(query).getResultList();
    }
    

    @Override
    public User getUserByCar(Car car) {
        User user = car.getUser();
        return em.find(User.class, user);
    }

    @Override
    public User getUserByInvoice(Invoice invoice) {
        User user = invoice.getUser();
        return em.find(User.class, user);
    }

    @Override
    public User getUserByUsername(String username) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<User> query = cb.createQuery(User.class);
        Root<User> invoice = query.from(User.class);
        
        List<Predicate> predicates = new ArrayList<>();
        predicates.add(cb.equal(invoice.get(User_.userName), username));
        
        query.where(predicates.toArray(new Predicate[predicates.size()]));
        if (!em.createQuery(query).getResultList().isEmpty())
        {
            return em.createQuery(query).getResultList().get(0);
        }
        else
        {
            User user = null;
            return user;
        }
    }

    @Override
    public User getUserByMail(String mail) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<User> query = cb.createQuery(User.class);
        Root<User> invoice = query.from(User.class);
        
        List<Predicate> predicates = new ArrayList<>();
        predicates.add(cb.equal(invoice.get(User_.email), mail));
        
        query.where(predicates.toArray(new Predicate[predicates.size()]));
        if (!em.createQuery(query).getResultList().isEmpty())
        {
            return em.createQuery(query).getResultList().get(0);
        }
        else
        {
            User user = null;
            return user;
        }
    }
}